package br.univas.vperiod.jowey.controller;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import br.univas.vperiod.jowey.dao.IPlayerDAO;
import br.univas.vperiod.jowey.entities.Player;
import br.univas.vperiod.jowey.entities.PlayerJob;


@Controller
@RequestMapping("player")
public class PlayerController {
	
	@Autowired
	private IPlayerDAO<?> playerDao;
	
	@RequestMapping("/list")
	public String list(){
		return "player";
	}
	
	@RequestMapping(value="/getPlayers",method = RequestMethod.GET)
	@ResponseBody
	public ArrayList<Player> getPlayers(){
		ArrayList<Player> players = new ArrayList<Player>();
		players = playerDao.findAll();
		
		for(Player pl : players){
			//System.out.println(pl.getEmail());
			//System.out.println(pl.getPosition().getIdPosition() + " DESC: " + pl.getPosition().getPositionDescription());
			pl.setPlayerJobs(null);
			pl.getPosition().setPlayers(null);
		}
		return players;
	}
	
	@RequestMapping(value="/getPlayersInProject",method = RequestMethod.GET)
	@ResponseBody
	public ArrayList<Player> getPlayersInProject(){
		ArrayList<PlayerJob> players = new ArrayList<PlayerJob>();
		players = playerDao.getPlayersInProjects();
		
		Player playerInProject = null;
		ArrayList<Player> playersInProjet = new ArrayList<Player>(); 
		ArrayList<Player> cathPlayers = new ArrayList<Player>();
		
		for(PlayerJob pj : players){
				if(!cathPlayers.contains(pj.getPlayer())){
					playerInProject = new Player();
					playerInProject.setRegistration(pj.getPlayer().getRegistration());
					playerInProject.setName(pj.getPlayer().getName());
					playerInProject.setEmail(pj.getPlayer().getEmail());
					playersInProjet.add(playerInProject);
					cathPlayers.add(pj.getPlayer());
				}
			
			
		}
			
		return playersInProjet;
	}
	
	@RequestMapping(value="/save",method = RequestMethod.POST)
    @ResponseBody
    public Player saveContact(Player player) {
		playerDao.save(player);
        return player;
    }
}
